package xiegd.study.eval;

import jdk.nashorn.api.scripting.NashornScriptEngine;
import lombok.extern.slf4j.Slf4j;

import javax.script.ScriptEngineManager;

/**
 * @Author: xieguangda
 * @Description:
 * @Date: 2023/4/11 16:03
 */
@Slf4j
public class MainTest {

    public static void main(String[] args) {
        Number recommendTechnicianNum = 0;
//        String calculationFormula = "12+(110.1-110)/20";

        String calculationFormula = "12+(currentScopeNum-110)/20";
        calculationFormula = calculationFormula
                .replaceAll("currentScopeNum","110.1");

        //ScriptEngine se = new ScriptEngineManager().getEngineByName("JavaScript");

        //创建脚本引擎管理
        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
        //获取脚本引擎管理
        NashornScriptEngine se = (NashornScriptEngine) scriptEngineManager.getEngineByName("nashorn");

        System.out.println("根据房屋计划获取推荐清洁人员数量，计算公式：【{}】"+ calculationFormula);
        try {
            recommendTechnicianNum = (Number) se.eval(calculationFormula);
        } catch (Exception e) {
            System.out.println("根据房屋计划获取推荐清洁人员数量，计算公式报错，计算公式：{}"+calculationFormula);
        }
        System.out.println("我是结果啊::" + recommendTechnicianNum);
    }

//    public static void main(String[] args) {
//        //创建脚本引擎管理
//        ScriptEngineManager scriptEngineManager = new ScriptEngineManager();
//        //获取脚本引擎管理
//        NashornScriptEngine scriptEngine = (NashornScriptEngine) scriptEngineManager.getEngineByName("nashorn");
//
//
//    }
}
